/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/8
 * Time: 16:47
 * Description:
 */
package com.hangkai.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.domain.Employee;
import com.hangkai.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台登录操作
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    //可以把登录成功的 员工 id 存在sessin  通过request.get方法 到session
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1 将页面提交的密码 password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2 根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3 如果没有查询到则返回登陆失败结果
        if(emp == null){
            return Result.error("登陆失败");
        }

        //4 密码比对 如果不一致 则返回登陆失败结果
        if(!emp.getPassword().equals(password)){
            return Result.error("登陆失败");
        }

        //5 查看员工状态 如果为已禁用转态 则返回员工已禁用结果
        if(emp.getStatus() == 0){
            return Result.error("账号已禁用");
        }
        //6 登陆成功 将员工id存入session中
        request.getSession().setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    /**
     * 登出操作
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return Result.success("登出成功!!");
    }

    /**
     * 添加员工信息
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工信息 {}", employee.toString());
        //设置初始密码 MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);
        return Result.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Employee>> getEmployeePage(int page, int pageSize, String name){
        log.info("page={}, pageSize={}, name={} ", page, pageSize, name);

        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        //添加过滤条件
        lqw.like(null != name, Employee::getUsername, name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lqw);
        return Result.success(pageInfo);
    }

    /**
     * 根据员工id 来修改信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> update(HttpServletRequest request, @RequestBody Employee employee){
//        log.info(employee.toString());

//        Long id = Thread.currentThread().getId();
//        log.info("当前线程id2 {}", id);

        Long temp_id = (Long) request.getSession().getAttribute("employee");
        if(1L == employee.getId()){
            //管理员为最高权限 不可修改
            return Result.error("管理员为最高权限 不可修改");
        }
//        employee.setUpdateUser(temp_id);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);

        return Result.success("员工修改成功!");
    }

    /**
     * 获得用户信息 在修改的时候 根据id查询
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployee(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        log.info("查询到的employee 是  {}", employee.toString());
        if (employee != null){
            return Result.success(employee);
        }
        return Result.error("没有查询到对应员工信息");
    }
}
